﻿using _15Net8AspNetCore_ORM.Context;
using _15Net8AspNetCore_ORM.Models;
using Dapper;
using Microsoft.AspNetCore.Mvc;

namespace _15Net8AspNetCore_ORM.OrderRespository
{
    public class OrderSqlSugarRespository : IOrderRespository
    {
        public SqlSugarDBContext _SqlSugarContext { get; set; }
        public OrderSqlSugarRespository(SqlSugarDBContext sqlSugarContext)
        {
            _SqlSugarContext = sqlSugarContext;
        }
        public Order AddOrder(Order order)
        {
            order.Id = _SqlSugarContext._SqlSugarClient.Insertable(order).ExecuteReturnIdentity();
            return order;
        }

        public void DeleteOrder(int id)
        {
            _SqlSugarContext._SqlSugarClient.Deleteable<Order>(id).ExecuteCommand();
        }

        public List<Order> GetAllOrder()
        {
            List<Order> orders = _SqlSugarContext._SqlSugarClient.Queryable<Order>().ToList();
            return orders;
        }

        public Order GetOrder(int id)
        {
            Order order = _SqlSugarContext._SqlSugarClient.Queryable<Order>().InSingle(id);
            return order;
        }

        public void UpdateOrder(int id, Order order)
        {
            _SqlSugarContext._SqlSugarClient.Updateable<Order>(order).ExecuteCommand();
        }
    }
}
